package org.apache.james.mime4j.parser;

import java.io.IOException;
import java.io.InputStream;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.descriptor.BodyDescriptor;

public class MimeStreamParser
{
  private boolean contentDecoding;
  private ContentHandler handler;
  private final MimeTokenStream mimeTokenStream;

  public MimeStreamParser()
  {
    this(null);
  }

  public MimeStreamParser(MimeEntityConfig paramMimeEntityConfig)
  {
    this.handler = null;
    if (paramMimeEntityConfig != null);
    try
    {
      localMimeEntityConfig = (MimeEntityConfig)paramMimeEntityConfig.clone();
      this.mimeTokenStream = new MimeTokenStream(localMimeEntityConfig);
      this.contentDecoding = false;
      return;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      while (true)
        localMimeEntityConfig = new MimeEntityConfig();
      localMimeEntityConfig = new MimeEntityConfig();
    }
  }

  public boolean isContentDecoding()
  {
    return this.contentDecoding;
  }

  public boolean isRaw()
  {
    return this.mimeTokenStream.isRaw();
  }

  public void parse(InputStream paramInputStream)
    throws MimeException, IOException
  {
    InputStream localInputStream;
    this.mimeTokenStream.parse(paramInputStream);
    int i = this.mimeTokenStream.getState();
    switch (i)
    {
    default:
      throw new IllegalStateException("Invalid state: " + i);
    case 12:
      BodyDescriptor localBodyDescriptor = this.mimeTokenStream.getBodyDescriptor();
      if (!(this.contentDecoding))
        break label164;
      localInputStream = this.mimeTokenStream.getDecodedInputStream();
      this.handler.body(localBodyDescriptor, localInputStream);
    case 11:
    case 5:
    case 1:
    case 7:
    case 9:
    case 4:
    case 8:
    case 2:
    case 10:
    case 3:
    case 0:
    case 6:
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        while (true)
                        {
                          while (true)
                          {
                            while (true)
                            {
                              while (true)
                              {
                                while (true)
                                  this.mimeTokenStream.next();
                                label164: localInputStream = this.mimeTokenStream.getInputStream();
                              }
                              this.handler.endBodyPart();
                            }
                            this.handler.endHeader();
                          }
                          this.handler.endMessage();
                        }
                        this.handler.endMultipart();
                      }
                      this.handler.epilogue(this.mimeTokenStream.getInputStream());
                    }
                    this.handler.field(this.mimeTokenStream.getField());
                  }
                  this.handler.preamble(this.mimeTokenStream.getInputStream());
                }
                this.handler.raw(this.mimeTokenStream.getInputStream());
              }
              this.handler.startBodyPart();
            }
            this.handler.startHeader();
          }
          this.handler.startMessage();
        }
        this.handler.startMultipart(this.mimeTokenStream.getBodyDescriptor());
      }
    case -1:
    }
  }

  public void setContentDecoding(boolean paramBoolean)
  {
    this.contentDecoding = paramBoolean;
  }

  public void setContentHandler(ContentHandler paramContentHandler)
  {
    this.handler = paramContentHandler;
  }

  public void setRaw(boolean paramBoolean)
  {
    this.mimeTokenStream.setRecursionMode(2);
  }

  public void stop()
  {
    this.mimeTokenStream.stop();
  }
}